﻿#!/usr/bin/env python
# -*- coding:utf-8 -*-

'a socket example which sends hello to client'

__author__='zhaoxp'

import socket,threading,time

def tcplink(sock,addr):
	print 'Accept new connection from %s:%s' % addr
	sock.send('Welcome!')
	while True:
		data=sock.recv(1024)
		time.sleep(1)
		if data=='exit' or not data:
			break
		sock.send('hello, %s' % data)
	sock.close()
	print 'Connection from %s:%s closed.' % addr

if __name__=='__main__':
	s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	s.bind(('127.0.0.1',9090))
	s.listen(5)
	print 'Waiting for connection'
	while True:
		print 'start to accept...'
		sock,addr=s.accept()
		print 'addr = ',addr
		t=threading.Thread(target=tcplink,args=(sock,addr))
		t.start()
	print '-----end-----'